package com.hou.shopping.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hou.shopping.product.entity.CategoryEntity;
import com.hou.shopping.product.service.CategoryService;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.R;


/**
 * @author hxj
 * @email houxiaojie1256@163.com
 * @date 2024-12-09 19:39:31
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 12.12 商品的三级分类
     *
     * @return
     */
    @RequestMapping("/list/tree")
    public R list() {
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }

    /**
     * 12.13 删除
     *
     * @RequestBody 获取请求体，必须发送POST请求、
     * SpringMVC自动将请求体中的数据json，转换为对象
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
//        1.检查当前删除的菜单，是否被别的地方引用   批量
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

    /**
     * 12.16
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);
        return R.ok();
    }

    /**
     * 信息回显
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
//        TODO：存在的问题，空对象可以提交，需要判断到底有没有写数据
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 1218  批量修改
     * @param category
     * @return
     */
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category) {
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }
}
